package com.apex.crm.service;

import com.apex.crm.base.BaseService;
import com.apex.crm.dao.CustomerLossMapper;
import com.apex.crm.dao.CustomerMapper;
import com.apex.crm.query.CustomerLossQuery;
import com.apex.crm.utils.AssertUtil;
import com.apex.crm.vo.CustomerLoss;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author XiaoYang
 * @version 1.0
 * @program: crm
 * @Classname CustomerLossService
 * @description TODO
 * @date 2021/6/9 14:46
 */

@Service
public class CustomerLossService extends BaseService<CustomerLoss,Integer> {

    @Resource
    private CustomerLossMapper customerLossMapper;

    @Resource
    private CustomerMapper customerMapper;

    //查询流失数据
    public Map<String, Object> queryCustomerLossByParams(CustomerLossQuery customerLossQuery) {
        Map<String,Object> map = new HashMap<String,Object>();
        PageHelper.startPage(customerLossQuery.getPage(),customerLossQuery.getLimit());
        PageInfo<CustomerLoss> pageInfo = new PageInfo<CustomerLoss>(customerLossMapper.selectByParams(customerLossQuery));
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return  map;
    }

    //添加流失顾客
    public void updateCustomerLossStateById(Integer id, String lossReason) {
        CustomerLoss customerLoss = customerLossMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(null == customerLoss,"待流失的客户记录不存在!");
        customerLoss.setState(0);// 0.确认流失
        customerLoss.setLossReason(lossReason);
        customerLoss.setConfirmLossTime(new Date());
        customerLoss.setUpdateDate(new Date());

        AssertUtil.isTrue(customerLossMapper.updateByPrimaryKey(customerLoss) < 1,"确认流失失败!");

    }
}
